// array.h

// Copyright (C) 2010  traxex

// Author: 	traxex <traxex@yahoo.cn>
// Created: 	18 Aug 2010
// Version: 	1.0

#pragma once

#include "../cuda_impl/fft.h"
namespace traxex{
namespace cuda{

template<typename T>__global__ void op_set_k( T *_, T t){
    _[_vi] = t;
}

template<typename T>class vector: refable< vector<T> >{
  public:


    uint	N;
    T *		_;
    bool	transed;    

    vector( uint n): N(n),_ (dmalloc<T>(n)), transed(false){}

    vector( ): N(0), _(0), transed(false){
    }

    vector operator~(){
        vector tmp = *this;
        tmp.transed = !transed;
        return tmp;
    }    
    
    vector& operator=( T t){
        op_set_k<<<cusize(N)>>>( _, t);
        return this;
    }

    virtual ~vector(){}

    void _finalize( ){
        if(_!=0) dfree( _);
    }
    __device__ T& operator()( uint n){
        return _[n];
    }
    
    vector clone(){
        vector tmp(N);
        d2dcpy( tmp._, _, N);
        return tmp;
    }

    vector& operator<<( const T *h){
	h2dcpy( _, h, N); 
        return *this;
    }
    vector& operator>>( T *h){	
        d2hcpy( h, _, N);
        return *this;
    }
  private:
    
};

template<typename T> void fft( vector<T> &out, vector<T> &in){
    _ensure( out.N==in.N, "can not do fft vector(%d)->vector(%d)", in.N, out.N);
    fftbase( out._, in._, in.N, -1);
}

template<typename T> void ifft( vector<T> &out, vector<T> &in){
    _ensure( out.N==in.N, "can not do fft vector(%d)->vector(%d)", in.N, out.N);
    fftbase( out._, in._, in.N, 1);
}

}
}
